package work5.StudentInfManagement;

import Uti.ScannerUti;

import java.util.Iterator;
import java.util.List;

public class StudentInfManage {
    public StudentInfManage() {
        while (true) {
            System.out.println("学生信息管理");
            System.out.println("--------------------------------");
            System.out.println("学生信息添加————1");
            System.out.println("学生信息修改————2");
            System.out.println("学生信息删除————3");
            System.out.println("学生信息查询————4");
            System.out.println("退出————0");
            int kk = ScannerUti.getInt();
            if (kk == 1) {
                addStudent();
            }  else if (kk == 2) {
                edit();
            }else if (kk == 3) {
                delete();
            }else if (kk == 4) {
                search();
            } else if (kk == 0) {
                break;
            }

        }

    }

//学生信息的新增

    public static void addStudent() {

        System.out.println("请输入学生姓名, 专业 以空格隔开");
        String[] s = (ScannerUti.getSting().split(" "));
        if(s.length<2){
            System.out.println("最后一条数据信息 输入有误");
            return;
        }
        Student student = null;
        String name = s[0];
        String major = s[1];
        student = new Student(0, name, major);
        StudentDao.insert(student);
       //printStudents(StudentDao.find());
        System.out.println("添加成功");

    }


//学生信息的删除

    public static void delete() {
        System.out.println("请输入学生编号：");
        int id = ScannerUti.getInt();
        List<Student> students = StudentDao.find(new Student(id));
        if(students.size()==0){
            System.out.println("没有这个学生！");
        }
        else {
            StudentDao.delete(new Student(id));
            printStudents(StudentDao.find(new Student(0)));
        }
    }

//一个学生信息的编辑

    public static void edit() {
        System.out.println("请输入学生编号：");
        int id = ScannerUti.getInt();
        List<Student> students = StudentDao.find(new Student(id));

        if(students.size()==0){
            System.out.println("没有这个学生！");
        }
        else {
            Student s=students.get(0);
            System.out.println("请输入学生姓名，专业（空格隔开）");
            String[] s1 = (ScannerUti.getSting().split(" "));
            s.setName(s1[0]);
            s.setMajor(s1[1]);
            StudentDao.change(s);
            printStudents(StudentDao.find(new Student(0)));
        }
    }

//学生信息的查询显示

    public static void search() {
        System.out.println("请输入学生编号（空格查询所有）:");
        String s = ScannerUti.getSting();
        if(s.equals(" ")){
            printStudents(StudentDao.find(new Student(0)));
        }else{
            int key = 0;
            try {
                key = Integer.parseInt(s);
            } catch (NumberFormatException e) {
                System.out.println("输入有误");
            }
            printStudents(StudentDao.find(new Student(key)));
        }

    }




//学生信息的打印显示

    public static void printStudents(List<Student> results) {
        System.out.println("编号\t\t姓名\t\t专业");
        System.out.println("--------------------------------");
        if (results.size() > 0) {
            Iterator<Student> iterator = results.iterator();
            while (iterator.hasNext()) {
                Student s = iterator.next();
                System.out.println(s);
            }
        } else {
            System.out.println("没有学生信息！！");
        }
        System.out.println("--------------------------------");
    }
}
